package com.jfinalnat.httpclient.sdk;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;

import com.chunyv.maas.pojo.token.TokenTimerHandler;
import com.jfinalnat.token.factory.TokenTimerHandlerFactory;

public class HttpCrossIntercepter implements HttpRequestInterceptor {

	public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
		
        if (PoolingHttpClientConfig.isEnable) {
        	
        	Header cookie = request.getLastHeader("Cookie");
        	TokenTimerHandler  tokenTimerHandler = TokenTimerHandlerFactory.createSSLSocketFactoryImpl(PoolingHttpClientConfig.amHost, PoolingHttpClientConfig.updateTime, PoolingHttpClientConfig.updateFastTime, PoolingHttpClientConfig.keyStorePath, PoolingHttpClientConfig.password);
        	if (null != cookie) {
        		String value = cookie.getValue();
        		value = "iPlanetDirectoryPro=" + tokenTimerHandler.getTokenURLEncode() + "; " + value;
        		request.setHeader("Cookie", value);

        	} else {
        		request.addHeader(new BasicHeader("Cookie", "iPlanetDirectoryPro="+tokenTimerHandler.getTokenURLEncode()));
        	}
        	
        }
	}

}
